import 'dart:convert';

import 'package:flutter/services.dart';

///http://coding.imooc.com/learn/questiondetail/135975.html
class MethodChannelManager {
  static const METHOD_CHANNEL_NAME = "MethodChannelPlugin";
  static MethodChannelManager _manager = MethodChannelManager._();
  MethodChannel channel;

  MethodChannelManager._() {
    channel = MethodChannel(
      METHOD_CHANNEL_NAME,
    );
    channel?.setMethodCallHandler(handler);
  }

  factory MethodChannelManager.getInstance() {
    return _manager;
  }

  Future invoke(String methodName, [dynamic args]) {
    return channel.invokeMethod(methodName, args);
  }

  Future<dynamic> handler(MethodCall call) async {
    print(
        "$METHOD_CHANNEL_NAME:  methodName:${call.method}  args${call.arguments.toString()}");
    switch (call.method) {
      case MethodTypes.INVOKE_DEMO:
        //android端用gson序列化 在dart端先反序列化再取值
        print(
            "INVOKE_DEMO: ${json.decode(call.arguments)["code"].runtimeType}");
        break;
      default:
        return Future.value("no such method");
        break;
    }
  }
}

class MethodTypes {
  static const INVOKE_DEMO = "invokeDemo";
  static const OPEN_AGENT_WEB = "openAgentWeb";
  static const CHECK_UPDATE = "checkUpdate";
  static const ERR_REPORT = "errReport";

}
